package android.slc.utils;

import android.slc.commonlibrary.CacheDiskUtils;
import android.slc.commonlibrary.CacheDoubleUtils;
import android.slc.commonlibrary.CacheMemoryUtils;
import android.slc.commonlibrary.GsonUtils;
import android.slc.utils.slc_cache.CacheExcludeField;
import android.slc.utils.slc_cache.CacheExclusionStrategy;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.util.List;
import java.util.Map;

/**
 * @author slc
 * @date 2022/09/19 下午 6:41
 */
public class SlcCacheUtils {

    public final static Gson slcCacheGson = new GsonBuilder()
            .serializeNulls()
            .disableHtmlEscaping()
            .addSerializationExclusionStrategy(new CacheExclusionStrategy(CacheExcludeField.class))
            .create();

    public synchronized static Gson getSlcCacheGson() {
        return slcCacheGson;
    }

    /**
     * 获取缓存文件
     *
     * @param cacheName
     * @return
     */
    public static File getCacheFile(String cacheName) {
        return getCacheFile("", cacheName);
    }

    public static File getCacheFile(String cacheDirParent, String cacheName) {
        File file = new File(getCachePath(cacheDirParent), cacheName);
        return file;
    }

    public static String getCachePath() {
        return getCachePath("");
    }

    public static String getCachePath(String cacheDirParent) {
        if (StringUtils.isEmpty(cacheDirParent)) {
            return SlcPathUtils.getAppCachePath();
        }
        return SlcPathUtils.getRootDir() + File.separator + cacheDirParent + File.separator + "cache";
    }

    public static <T> List<T> getList(CacheDoubleUtils cacheDoubleUtils, String key, Class<T> classType) {
        return GsonUtils.fromJson(getSlcCacheGson(), cacheDoubleUtils.getString(key, "[]"), GsonUtils.getListType(classType));
    }

    public static <T> List<T> getList(CacheDiskUtils cacheDiskUtils, String key, Class<T> classType) {
        return GsonUtils.fromJson(getSlcCacheGson(), cacheDiskUtils.getString(key, "[]"), GsonUtils.getListType(classType));
    }

    public static <T> List<T> getList(CacheMemoryUtils cacheMemoryUtils, String key, Class<T> classType) {
        return GsonUtils.fromJson(getSlcCacheGson(), cacheMemoryUtils.get(key, "[]"), GsonUtils.getListType(classType));
    }

    public static <T> Map<String, T> getMap(CacheDoubleUtils cacheDoubleUtils, String key, Class<T> classType) {
        return GsonUtils.fromJson(getSlcCacheGson(), cacheDoubleUtils.getString(key, "{}"), GsonUtils.getMapType(String.class, classType));
    }

    public static <T> Map<String, T> getMap(CacheDiskUtils cacheDiskUtils, String key, Class<T> classType) {
        return GsonUtils.fromJson(getSlcCacheGson(), cacheDiskUtils.getString(key, "{}"), GsonUtils.getMapType(String.class, classType));
    }

    public static <T> Map<String, T> getMap(CacheMemoryUtils cacheMemoryUtils, String key, Class<T> classType) {
        return GsonUtils.fromJson(getSlcCacheGson(), cacheMemoryUtils.get(key, "{}"), GsonUtils.getMapType(String.class, classType));
    }

    public static <T> T getObj(CacheDoubleUtils cacheDoubleUtils, String key, Class<T> classType) {
        return GsonUtils.fromJson(getSlcCacheGson(), cacheDoubleUtils.getString(key, "{}"), classType);
    }

    public static <T> T getObj(CacheDiskUtils cacheDiskUtils, String key, Class<T> classType) {
        return GsonUtils.fromJson(getSlcCacheGson(), cacheDiskUtils.getString(key, "{}"), classType);
    }

    public static <T> T getObj(CacheMemoryUtils cacheMemoryUtils, String key, Class<T> classType) {
        return GsonUtils.fromJson(getSlcCacheGson(), cacheMemoryUtils.get(key, "{}"), classType);
    }

    public static void saveData(CacheDoubleUtils cacheDoubleUtils, String key, Object data) {
        cacheDoubleUtils.put(key, GsonUtils.toJson(data));
    }

    public static void saveData(CacheDiskUtils cacheDiskUtils, String key, Object data) {
        cacheDiskUtils.put(key, GsonUtils.toJson(data));
    }

    public static void saveData(CacheMemoryUtils cacheMemoryUtils, String key, Object data) {
        cacheMemoryUtils.put(key, GsonUtils.toJson(data));
    }
}
